<?php
namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Hash;
use App\Models\Memberorder;
use App\Models\Users;
use App\Models\Usersprofile;
use App\Models\Corpuser;
use App\Models\Applylist;
use App\Services\OutputService;
use Exception,Log;

class UserController extends Controller
{
    public function usersProfile(Request $request)
    {
        if (empty($request->openid)) return OutputService::error("参数错误!");
        $userProfile = Usersprofile::usersProfileDetial($request->openid);
        if (empty($userProfile)) {
            Usersprofile::usersProfileAdd($request->openid,"","","","","","","","","");
            $userProfile = Usersprofile::usersProfileDetial($request->openid);
        }
        return OutputService::success("SUCCESS",["userProfile"=>$userProfile]);
    }

    public function usersProfileSubmit(Request $request)
    {
        if (empty($request->openid)) return OutputService::error("参数错误!");
        Usersprofile::usersProfileUpdate($request->openid,$request->name,$request->phone,$request->license,$request->idcard,$request->idcard_front,$request->idcard_back,$request->bank,$request->bank_front,$request->bank_back);
        return OutputService::success("SUCCESS");
    }

    public function orderList(Request $request)
    {
        if (empty($request->openid)) return OutputService::error("参数错误!");
        $orderLists = Memberorder::userMemberOrder($request->openid);
        return OutputService::success("SUCCESS",["orderLists"=>$orderLists]);
    }

    public function corpList(Request $request)
    {
        if (empty($request->openid)) return OutputService::error("参数错误!");
        $corpUsers = Corpuser::userCorpList($request->openid);
        return OutputService::success("SUCCESS",["corpUsers"=>$corpUsers]);
    }

    public function corpDelete(Request $request)
    {
        if (empty($request->openid) || empty($request->corp_id)) return OutputService::error("参数错误!");
        Corpuser::userCorpListDel($request->openid,$request->corp_id,$request->status);
        return OutputService::success("企业删除成功");
    }

    public function applyList(Request $request)
    {
        if (empty($request->openid)) return OutputService::error("参数错误!");
        $applyLists = Applylist::userApplyList($request->openid,$request->corp_id);
        return OutputService::success("SUCCESS",["applyLists"=>$applyLists]);
    }

    public function userSetting(Request $request)
    {
        if (empty($request->openid)) return OutputService::error("参数错误!");
        $users = Users::getUserByOpenid($request->openid);
        $nike = empty($request->nike) ? $users->nike : $request->nike;
        $head_img = empty($request->head_img) ? $users->head_img : $request->head_img;
        $phone = empty($request->phone) ? $users->phone : $request->phone;
        Users::updateSetting($request->openid,$nike,$head_img,$phone);
        if (empty($request->password)) {
            $password = Hash::make($request->password);
            Users::passwordUpdate($request->openid,$password);
        }
        return OutputService::success("更新成功!");
    }
}